package com.bkitmobile.kesu.android.GUI;

import com.bkitmobile.kesu.android.connectServer.Request;
import com.bkitmobile.kesu.android.connectServer.staticClass;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ChangePassDialog extends Dialog implements android.view.View.OnClickListener{
	private Context context;
	private EditText oldPass, newPass, confirmPass;
	private Button ok, cancel;
	
	public ChangePassDialog(Context context) {
		super(context);
		this.context = context;
	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_changepass);
		oldPass = (EditText)findViewById(R.id.setting_changePass_oldpass);
		newPass = (EditText)findViewById(R.id.setting_changePass_newpass);
		confirmPass = (EditText)findViewById(R.id.setting_changePass_confirmpass);
		ok = (Button)findViewById(R.id.setting_changepass_ok);
		cancel = (Button)findViewById(R.id.setting_changepass_cancel);
		ok.setOnClickListener(this);
		cancel.setOnClickListener(this);
		setTitle(R.string.title_dialog_changepass);
		setCancelable(true);
		

	}
 
	@Override
	public void onClick(View v) {
		String strOld, strNew, strConf;
		strOld = oldPass.getText().toString();
		strNew = newPass.getText().toString();
		strConf = confirmPass.getText().toString();
		
		if(v == ok){
			if(strOld.equals("") || strNew.equals("") || strConf.equals(""))
				return;
			if(!strNew.equals(strConf) || !strOld.equals(Login.getPass())){
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
		    	builder.setMessage(context.getString(R.string.setting_changepass_error))
		    	       .setNegativeButton("OK", new DialogInterface.OnClickListener() {
		    	           public void onClick(DialogInterface dialog, int id) {
		    	                dialog.cancel();
		    	           }
		    	       });
		    	AlertDialog alert = builder.create();
		    	alert.show();
		    	
			}else{
				String request =  "<changePwd>"+strNew;
				staticClass.socket.println(request);
				Login.setPass(strNew);
				dismiss();
			}
		}
		else
			dismiss();
		
		
	}
	
	public void show() {
		super.show();
		oldPass.setText("");
		newPass.setText("");
		confirmPass.setText("");
	}

}
